<script lang="ts" setup>
import type { FormInstance } from 'ant-design-vue';

import { onMounted, reactive, ref } from 'vue';

import { message } from 'ant-design-vue';

import { DICT_TYPE, dictUtils } from '#/utils/dict';

import EditMenuForm from './edit-menu-form.vue';

const emit = defineEmits(['success']);

export interface EditDictDataModalState {
  isUpdate: boolean;
  record: any;
}

const data = ref<EditDictDataModalState>({
  isUpdate: false,
  record: {},
});

const dictMap = reactive<any>({});

const form = reactive({
  type: '',
  name: '',
  value: '',
  status: 0,
  sort: 0,
  isDefault: false,
  extend1: '',
  extend2: '',
  colorType: '',
  customStyle: '',
  remark: '',
});

const formRef = ref<FormInstance>();

const isLoading = ref(false);

async function onSubmit() {
  await formRef.value!.validate();

  try {
    isLoading.value = true;

    message.success({
      content: `操作成功`,
    });

    emit('success');
  } catch (error) {
    console.error('表单验证失败:', error);
  } finally {
    isLoading.value = false;
  }
}

function initForm() {
  const defaultStatus = dictUtils.getDictDefaultObjByOptions(
    dictMap.value[DICT_TYPE.sysCommonStatus].data,
  )?.value;
  form.status = form.status ?? defaultStatus;

  const defaultColorType = dictUtils.getDictDefaultObjByOptions(
    dictMap.value[DICT_TYPE.sysCommonColorType].data,
  )?.value;
  form.colorType = form.colorType ?? defaultColorType;
}

onMounted(async () => {
  dictMap.value = await dictUtils.getDictDatasAsync([
    { type: DICT_TYPE.sysCommonStatus, valueType: 'number' },
    { type: DICT_TYPE.sysCommonWhether, valueType: 'number' },
    { type: DICT_TYPE.sysCommonColorType, valueType: 'string' },
  ]);
  initForm();
});
</script>
<template>
  <EditMenuForm ref="formRef" v-model:form="form" type="card" />
</template>
